using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using FireStarterModeller.BO;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// The <see cref="IBOSelectorAndEditor"/> for <see cref="DMUniqueConstraint"/>
    /// and the <see cref="UniqueConstraintPropertyInfosControl"/> used to edit the <see cref="DMUniqueConstraintProperty"/>
    /// </summary>
    public class UniqueConstraintInfosControl : PanelWin, IFormControl
    {
        private DMClass _dmClass;
        private readonly BOGridAndEditorControlWin<DMUniqueConstraint> _gridAndBOEditorControl;
        private readonly UniqueConstraintPropertyInfosControl _uniqueConstraintPropertyInfosControl;

        public UniqueConstraintInfosControl(IControlFactory controlFactory)
        {
            _gridAndBOEditorControl = new BOGridAndEditorControlWin<DMUniqueConstraint>(controlFactory, "default");
            _gridAndBOEditorControl.Width = 500;
            
            _uniqueConstraintPropertyInfosControl = new UniqueConstraintPropertyInfosControl(controlFactory);

            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_gridAndBOEditorControl, BorderLayoutManager.Position.West);
            layoutManager.AddControl(_uniqueConstraintPropertyInfosControl, BorderLayoutManager.Position.Centre);

            _gridAndBOEditorControl.BusinessObjectSelected +=
                (sender, e) => _uniqueConstraintPropertyInfosControl.UniqueConstraint = e.BusinessObject;
            UpdateState();
        }
        /// <summary>
        /// The <see cref="DMClass"/> whose <see cref="DMUniqueConstraint"/>s are being edited by this control
        /// </summary>
        public DMClass Class
        {
            get { return _dmClass; }
            set
            {
                _dmClass = value;
                BusinessObjectCollection<DMUniqueConstraint> col = _dmClass == null 
                        ? new BusinessObjectCollection<DMUniqueConstraint>() 
                        : _dmClass.UniqueConstraints;
                _gridAndBOEditorControl.BusinessObjectCollection = col;
                UpdateState();
            }
        }

        private void UpdateState()
        {
            this.Enabled = _dmClass != null;
        }
        /// <summary>
        /// The <see cref="IBusinessObjectCollection"/> of <see cref="DMUniqueConstraint"/>s being managed by this control
        /// </summary>
        public BusinessObjectCollection<DMUniqueConstraint> UniqueConstraintInfos
        {
            get { return (BusinessObjectCollection<DMUniqueConstraint>)_gridAndBOEditorControl.GridControl.Grid.BusinessObjectCollection; }
        }
        /// <summary>
        /// The <see cref="DMUniqueConstraint"/> currently selected in the Grid (i.e. currently being edited)
        /// </summary>
        public DMUniqueConstraint UniqueConstraint
        {
            get { return _gridAndBOEditorControl.CurrentBusinessObject; }
            set { _gridAndBOEditorControl.CurrentBusinessObject = value; }
        }
        /// <summary>
        /// The <see cref="UI.UniqueConstraintPropertyInfosControl"/> being used to Manage the 
        /// currently selected  <see cref="DMUniqueConstraint"/>'s
        /// <see cref="DMUniqueConstraintProperty"/>s
        /// </summary>
        public UniqueConstraintPropertyInfosControl UniqueConstraintPropertyInfosControl
        {
            get { return _uniqueConstraintPropertyInfosControl; }
        }

        public void SetForm(IFormHabanero form)
        {

        }
    }
}
